package com.project2.shiro;

import com.project2.bean.po.*;
import com.project2.mapper.MarketAdminMapper;
import com.project2.mapper.MarketPermissionMapper;
import com.project2.mapper.MarketRoleMapper;
import com.project2.mapper.MarketUserMapper;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.AuthenticationInfo;
import org.apache.shiro.authc.AuthenticationToken;
import org.apache.shiro.authc.SimpleAuthenticationInfo;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.authz.SimpleAuthorizationInfo;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.subject.PrincipalCollection;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;

/**
 * TODO
 *
 * @param
 * @author wj
 * @return
 */
@Component
public class WxRealm extends AuthorizingRealm {


    @Autowired
    MarketUserMapper marketUserMapper;

    @Override
    protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principalCollection) {

        return null;
    }

    //做认证的
    @Override
    protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken authenticationToken) throws AuthenticationException {
        MarketToken marketToken = (MarketToken) authenticationToken;
        String username = (String) authenticationToken.getPrincipal();
        String type = marketToken.getType();
        //admin的认证
        MarketUserExample marketUserExample = new MarketUserExample();
        MarketUserExample.Criteria criteria = marketUserExample.createCriteria();
        if (username!=null&&!"".equals(username)){
            criteria.andUsernameEqualTo(username);
        }
        List<MarketUser> marketUsers = marketUserMapper.selectByExample(marketUserExample);
        if (marketUsers.size()>0){
            MarketUser marketUser = marketUsers.get(0);
            return new SimpleAuthenticationInfo(marketUser,marketUser.getPassword(),getName());
        }

        return null;
    }
}
